//
//  GGTMyFavoriteViewController.m
//  GGT
//
//  Created by Vision on 14-11-11.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTMyFavoriteViewController.h"
#import "GGTDataBaseHandler.h"
#import "GGTTeamBuyDataModel.h"
#import "GGTTeamBuyTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "GGTTeamBuyModel.h"
#import "GGTTeamBuyDetailViewController.h"
@interface GGTMyFavoriteViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *myFavoriteTableView;
@property (nonatomic, retain) NSMutableArray *myDateArr;
@property (nonatomic, assign) NSInteger flag;
@end

@implementation GGTMyFavoriteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.myDateArr = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    GGTDataBaseHandler *hander = [GGTDataBaseHandler shareInstance];
    self.myDateArr = [hander selectAll];
    self.myFavoriteTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myFavoriteTableView.delegate = self;
    self.myFavoriteTableView.dataSource =self;
    self.myFavoriteTableView.rowHeight = 120;
    [self.view addSubview:self.myFavoriteTableView];
    [self.myFavoriteTableView registerClass:[GGTTeamBuyTableViewCell class] forCellReuseIdentifier:@"myFavoriteTableView"];
    [self.myFavoriteTableView reloadData];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteButtonClicked:)];
    
}
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView
            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return self.flag == 0 ? UITableViewCellEditingStyleDelete:
    UITableViewCellEditingStyleInsert;
}

- (void)deleteButtonClicked:(UIBarButtonItem *)add
{
    self.flag = 0;
    if ([[add title] isEqualToString: @"删除"]) {
        [add setTitle:@"完成"];
        
        [self.myFavoriteTableView setEditing:YES animated:YES];
    } else
    {
        [add setTitle:@"删除"];
        [self.myFavoriteTableView setEditing:NO animated:YES];
    }
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        
        GGTDataBaseHandler *hander = [GGTDataBaseHandler shareInstance];
        [hander deleteTeamBuyDataLieming:@"deal_id" chaxunzhi:[NSString stringWithFormat:@"'%@'",[[self.myDateArr objectAtIndex:indexPath.row] deal_id]]];
        [self.myDateArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    GGTTeamBuyDetailViewController *detail = [[GGTTeamBuyDetailViewController alloc]init];
    GGTTeamBuyDataModel *tempModel = self.myDateArr[indexPath.row];
    GGTTeamBuyDetailModel * tempPassModel = [[GGTTeamBuyDetailModel alloc]init];
    tempPassModel.deal_id = tempModel.deal_id;
    tempPassModel.number = tempModel.number;
    detail.GGTTeamBuyDetail = tempPassModel;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myDateArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    GGTTeamBuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myFavoriteTableView"];
    GGTTeamBuyDataModel *temp = self.myDateArr[indexPath.row];
    GGTTeamBuyModel *otherModel = [[GGTTeamBuyModel alloc]init];
    otherModel.title = temp.title;
    otherModel.descript = temp.descript;
    otherModel.current_price = temp.current_price;
    otherModel.list_price = temp.list_price;
    otherModel.image = temp.image;
    otherModel.deal_id = temp.deal_id;
    cell.GGTTeamBuy = otherModel;
    return cell;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
